package com.zxx.coolweather._wuguan2.queue_prority;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import androidx.annotation.NonNull;
import androidx.arch.core.executor.DefaultTaskExecutor;
import androidx.arch.core.executor.TaskExecutor;

/**
 * 先进先出的队列
 *
 * @author zxx on 2022/1/12
 */
public class YolandaLinkedQueue {

    private BlockingQueue<Request> blockingQueue;
    private TaskExecutorMy[] taskExecutor;

    public YolandaLinkedQueue(int poolSize) {
        // LinkedBlockingQueue是一个先进先出的队列
        blockingQueue = new LinkedBlockingQueue<>();
        taskExecutor = new TaskExecutorMy[poolSize];
    }

    public void add(Request request){
        blockingQueue.add(request);
    }

    public void start(){
        for (int i = 0; i < taskExecutor.length; i++) {
            taskExecutor[i] = new TaskExecutorMy(blockingQueue);
            taskExecutor[i].start();
        }
    }

    public void stop(){
        for (TaskExecutorMy taskExecutorMy : taskExecutor) {
            taskExecutorMy.setRunning(false);
            taskExecutorMy.interrupt();
        }
    }

}
